/**
 * Created by leiweibo on 6/28/15.
 */

var app = angular.module('usercontroller', []);

app.filter('offset', function () {
  return function (input, start) {
    start = parseInt(start, 10);
    return input.slice(start);
  };
});

app.controller('UserCtrl', function ($scope, Main, HospitalService, ProvinceService, UserData) {
    $scope.roles = [
      {code: 1, desc: '管理员'},
      {code: 2, desc: '财务'},
      {code: 3, desc: '销售人员'},
      {code: 4, desc: '外派人员'}
    ];o

    $scope.displayed = [];

    $scope.callServer = function (tableState) {
      $scope.isLoading = true;

      var pagination = tableState.pagination;
      var pageSize = pagination.number || 20;
      var pageNo = Math.ceil((pagination.start + 1) / pageSize) || 1;

      Main.getPage(pageNo, pageSize, tableState, function (res) {
        $scope.displayed = res.users;
        tableState.pagination.numberOfPages = res.pageNumber;
        $scope.isLoading = false;
      });
    };

    $scope.createOrUpdateUser = function () {

      var formData = {
        id: $scope.id,
        username: $scope.username,
        realname: $scope.realname,
        role: $scope.role,
        email: $scope.email,
        phone: $scope.phone
      }

      if ($scope.hospital) {
        formData.hospital = $scope.hospital
      }

      if ($scope.id) {
        Main.updateUser(formData, function (res) {
          if (res.result == true) {
            window.location = "#/user_list";
            alert('添加用户成功');
          } else {
            $scope.error_msg = res.message;
          }
        }, function (data, status) {
          alert('服务器出错，请刷新重试!');
        })
      } else {
        Main.createUser(formData, function (res) {
          if (res.result == true) {
            delete $scope.username
            delete $scope.realname
            delete $scope.role
            delete $scope.email
            delete $scope.phone
            delete $scope.province
            delete $scope.hospital

          } else {
            $scope.error_msg = res.message;
          }
        }, function (data, status) {
          alert('服务器出错，请刷新重试!');
        })
      }
    };

    $scope.rolechange = function(role) {
      if (role == 4) {
        $('#hospital_field').removeClass('hidden')
      } else {
        $('#hospital_field').addClass('hidden')
      }
    }

    $scope.provinces = ProvinceService.list(function(data) {
      $scope.provinces = data.provinces
    });

    $scope.provinceChanged = function(provinceId) {
      $scope.hospitals = HospitalService.list_by_province(provinceId, function(res) {
        if (res.result == false) {
          $scope.error_msg = res.message;
        }
        $scope.hospitals = res.hospitals;
      });
    }

    $scope.removeItem = function (pos) {
      bootbox.setDefaults("locale", "zh_CN");
      bootbox.confirm("确定删除该用户吗", function (result) {
        if (result == true) {
          var user = $scope.displayed[pos];
          if (user) {
            var data = {id: user.id};
            Main.deleteUser(data, function (res) {
              if (res.result == true) {
                $scope.displayed.splice(pos, 1);
              } else {
                alert('删除数据失败');
              }
            }, function (data, status) {
            });
          } else {
            alert("请刷新重试");
          }
        }
      });
    };

    $scope.cancel = function() {
      window.location = '#/user_list';
    }

    $scope.addUser = function () {
      UserData.set(null);
      window.location = "#/create_user";
    }

    $scope.editUser = function (pos) {
      var targetUser = $scope.displayed[pos];
      UserData.set(targetUser);
      window.location = "#/update_user";
    };

    if (UserData.get()) {
      $scope.realname = UserData.get().realname;
      $scope.username = UserData.get().username;
      $scope.email = UserData.get().email;
      $scope.phone = UserData.get().phone;
      $scope.id = UserData.get().id;
      $scope.role = UserData.get().role;

      if ($scope.id && $scope.role == 4) {
        $('#hospital_field').removeClass('hidden')
      } else {
        $('#hospital_field').addClass('hidden')
      }
      if (UserData.get().hospital) {
        HospitalService.list_by_province(UserData.get().hospital.province, function(res) {
          if (res.result == false) {
            $scope.error_msg = res.message;
          }
          $scope.hospitals = res.hospitals;
        });
        $scope.province = UserData.get().hospital.province;
        $scope.hospital = UserData.get().hospital.id;
      }
    }
  })